package cpa.prowon.gui.actions;

import java.util.Iterator;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;

import pho.prowon.category.RootIndexCategory;
import cpa.prowon.Application;
import cpa.prowon.gui.MainWindow;
import cpa.prowon.image.ImageContainer;

/**
 * The remove action is used to remove an image or multiple images
 * from the index
 * @author Chris
 */
public class RemoveAction
	extends PatchedAction
	implements ISelectionChangedListener {

	/**
	 * Constructor
	 * @param MainWindow The window where the action is used
	 * @author
	 */
	public RemoveAction(MainWindow w) {
		super(w);
		setEnabled(false);
	}

	/**
	 * Return the title key
	 * @return The title key
	 * @author Chris
	 */
	protected String getTitleKey() {
		return "menu_item_edit_remove";
	}

	/**
	 * Return the tooltip key
	 * @return The tootip key
	 * @author Chris
	 */
	protected String getTooltipKey() {
		return "menu_item_edit_remove_tooltip";
	}

	/**
	 * Return the image key
	 * @return The image key
	 * @author Chris
	 */
	protected String getImageKey() {
		return "file:icons/close.gif";
	}

	/**
	 * Return title for the message box
	 * @return The message box title
	 * @author Chris
	 */
	protected String getMessageBoxTitle() {
		return Application.getInstance().getResourceBundle().getString("title_removeimages");
	}

	/**
	 * Return message for the message box
	 * @return The message box message
	 * @author Chris
	 */
	protected String getMessageBoxMessage() {
		return Application.getInstance().getResourceBundle().getString("msg_removeimages");
	}

	/**
	 * Run the action
	 * @author Chris
	 */
	public void run() {
		
		// Ask the user whether he is really sure to remove the selected image(s) the index
		MessageBox msgBox = new MessageBox(window.getShell(), SWT.OK | SWT.CANCEL);
		msgBox.setText(getMessageBoxTitle());
		msgBox.setMessage(getMessageBoxMessage());
		int answer = msgBox.open();
		
		// If the user has chosen no, then return
		if (answer != SWT.OK) {
			return;
		}
		
		// Get the selected image(s)
		IStructuredSelection selection = window.getPictureListSelection();
		
		// Remove the images from the index
		RootIndexCategory ic = window.getCategoryPart().getRootIndexCategory();
		Iterator ii = selection.iterator();
		while (ii.hasNext()) {
			ImageContainer image = (ImageContainer) ii.next();
			removeImage(ic, image);
		}

		// Refresh the category viewer, in case the category is not needed anymore, it will get removed
		window.refreshCategoryPart();
				
		// Refresh the picture list viewer
		window.refreshListPart();
		
		// Save the index
		ic.writeIndex();
			
	}
	
	/**
	 * Do the actual removal of the image
	 * @param RootIndexCategory The root index category which is the current index
	 * @param ImageContainer The image to be removed
	 * @author Chris
	 */
	protected void removeImage(RootIndexCategory ic, ImageContainer image) {
		ic.removeImage(image);
	}

	/**
	 * Listen to the selection changed event in order to enable or disable the action
	 * @param SelectionChangedEvent The event that occurs when the selection changed
	 * @author Chris
	 */
	public void selectionChanged(SelectionChangedEvent event) {

		// Get the selection and set the action enabled or disabled depending on this
		IStructuredSelection selection = window.getPictureListSelection();
		if (selection.size() == 0) {
			setEnabled(false);
			return;
		}
		setEnabled(true);

	}
	
}
